import axios from "axios";
import NProgress from 'nprogress'
/* 根路径 */
axios.defaults.baseURL = 'http://localhost:8080';
// axios.defaults.headers.common['Authorization'] = sessionStorage.getItem('token') ? sessionStorage.getItem('token') : '';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

/* 配置拦截器，在config中给header添加Authorization */
axios.interceptors.request.use((config) => {
    NProgress.start();
	config.headers.Authorization = sessionStorage.getItem('token') ? sessionStorage.getItem('token') : ''
    return config
})

axios.interceptors.response.use((config) => {
    NProgress.done();
    return config
})

/* 获取轮播图 */
export const getBanner = () => {
    return axios.get('banner');
}
/* 获取推荐歌单 */
export const getPersonalized = (limit) => {
    return axios.get('/personalized',{
        params:{
            limit
        }
    });
}
/* 获取歌单详情 */
export const getPlayListDetail = (id) => {
    return axios.get('/playlist/detail',{
        params:{
            id
        }
    })
}

/* 获取歌单内的歌曲 */
export const getPlayDetailMusicList = (id,limit) => {
    return axios.get('/playlist/track/all',{
        params:{
            id,limit
        }
    })
}
/* 获取热门歌单评论 */
export const getHotComment = (id,type,limit) => {
    return axios.get('/comment/hot',{
        params:{
            id,type,limit
        }
    })
}
/*获取评论
0: 歌曲 1: mv 2: 歌单 3: 专辑 4: 电台节目 5: 视频 6: 动态 7: 电台
pageNo:分页参数,第 N 页,默认为 1

pageSize:分页参数,每页多少条数据,默认 20

sortType: 排序方式, 1:按推荐排序, 2:按热度排序, 3:按时间排序

cursor: 当sortType为 3 时且页数不是第一页时需传入,值为上一条数据的 time
*/
export const getComment = ({id,type,pageNo,pageSize,sortType,cursor}) => {
    return axios.get('/comment/new',{
        params:{
            id,type,pageNo,pageSize,sortType,cursor
        }
    })
}

/* 获取歌单收藏者 */
export const getSuberList = ({id,limit = 30,offset}) => {
    return axios.get('/playlist/subscribers',{
        params:{
            id , limit , offset
        }
    })
}

/* 检索音乐  */
export const getSearchMusic = ({limit,offset,type,keywords}) => {
    return axios.get('/cloudsearch',{
        params:{
            limit,offset,type,keywords
        }
    })
}

/* 热搜 */
export const getHotSearch = () =>{
    return axios.get('/search/hot/detail');
}

/* 搜索建议 */
export const getSuggestSearch = ({type,keywords}) => {
    return axios.get('/search/suggest',{
        params:{
            type,keywords
        }
    })
}
/* 获取专辑内容 */
export const getAlbumDetail = (id) => {
    return axios.get('/album',{
        params:{
            id
        }
    })
}
/* 获取专辑动态内容，评论，收藏 */
export const getAlbumDynamic = (id) => {
    return axios.get('/album/detail/dynamic',{
        params:{
            id
        }
    })
}
/* 歌手详情 */
export const  getArtistDetail =(id) => {
    return axios.get('/artist/detail',{
        params:{
            id
        }
    })
}
/* 歌手热门50首 */
export const getArtistTop = (id) => {
    return axios.get('/artist/top/song',{
        params:{
            id
        }
    })
}
/* 获取歌手专辑 */
export const getArtistAlbum = ({id,limit,offset}) =>{
    return axios.get('/artist/album',{
        params:{
            id,limit,offset
        }
    })
}
/* 获取歌手mv */
export const getArtistMV = (id) => {
    return axios.get('/artist/mv',{
        params:{
            id
        }
    })
}
/* 获取热门歌手 */
export const getTopArtist = (limit = 20 , offset = 0) => {
    return axios.get('/top/artists',{
        params:{
            limit,offset
        }
    })
}
/* 获取歌手描述 */
export const getArtistIntro = (id) => {
    return axios.get('/artist/desc',{
        params:{
            id
        }
    })
}
/* 获取用户详情 */
export const getUserDetail = (uid) => {
    return axios.get('/user/detail',{
        params:{
            uid
        }
    })
}
/* 获取用户歌单 */
export const getUserPlayList = (uid, offset , limit = 30) => {
    return axios.get('/user/playlist',{
        params:{
            uid,offset,limit
        }
    })
}

/* 获取MV详情 */
export const getMvDetail = (type , id) => {
    if(type === 'v'){
        return axios.get('/video/detail',{
            params:{
                id
            }
        })
    } else if( type === 'mv'){
        return axios.get('/mv/detail',{
            params:{
                mvid:id
            }
        })
    } else {
        return {code :404}
    }
}

/* 获取视频/MV的URL */
export const getVideoUrl = (type,id) => {
    if(type === 'v'){
        return axios.get('/video/url',{
            params:{
                id
            }
        })
    } else if(type === 'mv'){
        return axios.get('/mv/url',{
            params:{
                id
            }
        })
    } else {
        return {code : 404}
    }
}
/* 获取视频/mv点赞评论数据 */
export const getVideoInfo = (type,id) => {
    if(type === 'v'){
        return axios.get('/video/detail/info',{
            params:{
                vid:id
            }
        })
    } else if(type === 'mv'){
        return axios.get('/mv/detail/info',{
            params:{
                mvid:id
            }
        })
    } else {
        return {code : 404}
    }
}

/* 相关视频 */
export const getRelatedVideo = (type , id) => {
    return axios.get('/related/allvideo',{
        params:{
            id
        }
    })
}

export const getRelatedMv = () => {
   return axios.get('/personalized/mv');
}       

/* 手机密码登录 */
export const doLogin = ({phone , md5_password ,captcha}) => {
    return axios.get('/login/cellphone',{
        params:{
            phone , md5_password ,captcha
        }
    })
}
/* 二维码登录 */
// 生成二维码key
export const getQrKey = () => {
    return axios.get('/login/qr/key');
}
// 生成二维码base64
export const createQr = (key , qrimg = true) => {
    return axios.get('/login/qr/create',{
        params:{
            key ,qrimg
        }
    })
}
// 轮询二维码状态 800 为二维码过期,801 为等待扫码,802 为待确认,803 为授权登录成功(803 状态码下会返回 cookies)
export const checkQr = (key) => {
    return axios.get('/login/qr/check',{
        params:{
            key
        }
    })
}
/* 验证码登录 */
export const getCode = (phone) => {
    return axios.get('/captcha/sent',{
        params:{
            phone
        }
    })
}

/* 获取账号信息 */
export const getAcount = () => {
    return axios.get('/user/account');
}

/* 退出登录 */
export const logout = () => {
    return axios.get('/logout');
}

/* 获取登录后喜欢的音乐 */
export const getLikeIdList = (uid) => {
    return axios.get('/likelist',{
        params:{
            uid
        }
    })
}

/* 登录后收藏歌单 1：收藏 2：取消收藏 */
export const setPlaylistSub = (id , t) => {
    return axios.get('/playlist/subscribe',{
        params:{
            id , t
        }
    })
}

/* 发送删除评论  t:1 发送, 2 回复
type: 数字,资源类型,对应歌曲,mv,专辑,歌单,电台,视频对应以下类型
0: 歌曲 1: mv 2: 歌单 3: 专辑 4: 电台 5: 视频 6: 动态  id:对应资源 id  content :要发送的内容 commentId :回复的评论 id (回复评论时必填)  
删除评论 必选参数
t:0 删除 type: 数字,资源类型,对应歌曲,mv,专辑,歌单,电台,视频对应以下类型*/
export const sendComment = ({t , type , id , content , commentId}) => {
    return axios.get('/comment',{
        params:{
            t , type , id , content , commentId
        }
    })
}

/* 登录后点赞  id : 资源 id, 如歌曲  id,mv id   cid : 评论 id  t : 是否点赞 , 1 为点赞 ,0 为取消点赞 type: 数字 , 资源类型 , 对应歌曲 , mv, 专辑 , 歌单 , 电台, 视频对应以下类型*/
export const likeComment = ({id , cid , t , type}) => {
    return axios.get('/comment/like',{
        params:{
            id , cid , t , type
        }
    })
}
//视频mv点赞
export const likeVideo = ({id  , t = 1 , type}) => {
    return axios.get('/resource/like',{
        params:{
            id ,  t , type
        }
    })
}


/* 收藏/取消收藏专辑 */
export const setAlbumSub = (id , t) => {
    return axios.get('/album/sub',{
        params:{
            id , t
        }
    })
}
/* 收藏/取消收藏歌手 */
export const setArtistSub = (id , t) => {
    return axios.get('/artist/sub',{
        params:{
            id , t
        }
    })
}

/* 收藏mv视频 */
export const subVideo = ({type , t , id}) => {
    if(type === 'v'){
        return axios.get('/video/sub',{
            params:{
                t , id
            }
        })
    } else if(type === 'mv'){
        return axios.get('/mv/sub',{
            params:{
                t , mvid:id
            }
        })
    } else {
        return {code :404}
    }
}

/* 喜欢音乐 */
export const likeMusic = (id , like = true) => {
    return axios.get('/like?' ,{
        params:{
            id ,like
        }
    })
}

/*  获取音乐url*/
export const getMusicUrl = (id) => {
    return axios.get('/song/url',{
        params:{
            id
        }
    })
}

/* 获取歌词 */
export const getLyric = (id) => {
    return axios.get('/lyric',{
        params:{
            id
        }
    })
}
//获取精品歌单封面
export const gethighquality = (limit , cat , before) => {
    return axios.get('/top/playlist/highquality',{
        params:{
            limit , cat , before
        }
    })
}
/* 获取歌单热门分类 */
export const getHotCat = () => {
    return axios.get('/playlist/hot');
}
/* 获取歌单所有分类 */
export const getAllCat = () => {
    return axios.get('/playlist/catlist')
}
/* 根据分类获取歌单 */
export const getPlayListByCat = ({limit , order , cat , offset}) => {
    return axios.get('/top/playlist',{
        params:{
            limit , order , cat , offset
        }
    })
}
/* 获取排行榜信息 */
export const getTopList = () => {
    return axios.get('/toplist');
}
/* 获取歌手列表 */
export const getArtistList = ({limit , offset , type , area , initial}) => {
    return axios.get('/artist/list',{
        params:{
            limit , offset , type , area , initial
        }
    })
}
/* 新歌推荐 */
export const getTopMusic = (type) => {
    return axios.get('/top/song',{
        params:{
            type
        }
    })
}
/* 本周专辑 */
export const getTopAlbum = ({  area, type, year, month }) => {
    return axios.get('/top/album',{
        params:{
             area, type, year, month 
        }
    })
}
/* 添加歌曲到自己歌单 */
export const musicToPlayList = ({op,pid,tracks}) => {
    return axios.get('/playlist/tracks',{
        params:{
            op,pid,tracks
        }
    })
}
/* 视频分类 */
export const getVideoAllTag = () => {
    return axios.get('/video/group/list');
}
/* 热门分类 */
export const getVideoHotTag = () => {
    return axios.get('/video/category/list');
}
/* 获取全部视频列表 */
export const getAllVideo = (offset) => {
    return axios.get('/video/timeline/all',{
        params:{
            offset
        }
    })
}
/* 根据标签获取视频列表 */
export const getVideoByTab = (id , offset) => {
    return axios.get('/video/group',{
        params:{
            id , offset
        }
    })
}
/* 最新mv */
export const getNewMv = (area , limit) => {
    return axios.get('/mv/first',{
        params:{
            area , limit
        }
    })
}
/*网易出品MV  */
export const getNetMv = (limit , offset) => {
    return axios.get('/mv/exclusive/rcmd',{
        params:{
            limit , offset
        }
    })
}
/* mv排行 */
export const getTopMvList = ({area , offset = 0 , limit}) => {
    return axios.get('/top/mv',{
        params:{
            area , offset , limit
        }
    })
}
/* 全部mv */
export const getAllMv = ({area , type , order , limit , offset}) => {
    return axios.get('/mv/all',{
        params:{
            area , type , order , limit , offset
        }
    })
}
/* 获取私人FM */
export const getPersonalFm = () => {
    return axios.get('/personal_fm')
}
/* 私人垃圾桶 */
export const fmTrash =(id) => {
    return axios.get('/fm_trash',{
        params:{
            id
        }
    })
}
/* 获取每日推荐 */
export const getRecomSong = () => {
    return axios.get('/recommend/songs')
}
/* 获取收藏的专辑 */
export const getSubAlbum = (offset = 0 , limit = 25) => {
    return axios.get('/album/sublist',{
        params:{
            offset , limit
        }
    })
}
/* 获取收藏歌手的列表 */
export const getSubArtists = () => {
    return axios.get('/artist/sublist')
}
/* 获取收藏的mv */
export const getSubMv = () => {
    return axios.get('/mv/sublist')
}
/* 修改用户信息 */
export const updateUserInfo =({gender, signature, city, nickname, birthday, province}) => {
    return axios.get('/user/update',{
        params:{
            gender, signature, city, nickname, birthday, province
        }
    })
}
/* 上传图片 */
export const updloadAvatar = ({imgSize , data , imgX = 0 , imgY = 0}) => {
    return axios.post(`/avatar/upload?imgSize=${imgSize}&imgX=${imgX}&imgY=${imgY}&timestamp=${Date.now()}`,
    data,
    {
        'Content-Type': 'multipart/form-data',
    })
}